package com.kyh.util;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

	/** 
     * 首字母大写 
     *  
     * @param srcStr 
     * @return 
     */  
    public static String firstCharacterToUpper(String srcStr) {  
        return srcStr.substring(0, 1).toUpperCase() + srcStr.substring(1);  
    }

	/** 
     * 首字母小写
     *  
     * @param srcStr 
     * @return 
     */  
    public static String firstCharacterToLower(String srcStr) {  
        return srcStr.substring(0, 1).toLowerCase() + srcStr.substring(1);  
    }
    
    /**
    * 去除首尾指定字符
    * @param str 字符串
    * @param element 指定字符
    * @return
    */
    public static String trimFirstAndLastChar(String str, String element){
        boolean beginIndexFlag = true;
        boolean endIndexFlag = true;
        do{
            int beginIndex = str.indexOf(element) == 0 ? 1 : 0;
            int endIndex = str.lastIndexOf(element) + 1 == str.length() ? str.lastIndexOf(element) : str.length();
            str = str.substring(beginIndex, endIndex);
            beginIndexFlag = (str.indexOf(element) == 0);
            endIndexFlag = (str.lastIndexOf(element) + 1 == str.length());
        } while (beginIndexFlag || endIndexFlag);
        return str;
    }
    
	/** 
     * 替换字符串并让它的下一个字母为大写 
     *  
     * @param srcStr 
     * @param org 
     * @param ob 
     * @return 
     */  
    public static String replaceUnderlineAndfirstToUpper(String srcStr) {
    	String org = "_";
        String newString = "";  
        int first = 0;  
        while (srcStr.indexOf(org) != -1) {  
            first = srcStr.indexOf(org);  
            if (first != srcStr.length()) {  
                newString = newString + srcStr.substring(0, first);  
                srcStr = srcStr.substring(first + org.length(), srcStr.length());  
                srcStr = firstCharacterToUpper(srcStr);  
            }  
        }  
        newString = newString + srcStr;  
        return newString;  
    }
    
    /**
     * 真实姓名处理，如黄哈哈->黄**
     * @param contacts
     * @return
     */
    public static String contactsReplace(String contacts) {
    	int length = contacts.length();
    	
    	if(length == 2){
    		return contacts.substring(0, 1) + "*";
    	}
    	
    	if(0 != length){
    		String tmp = contacts.substring(0, 1);
    		
    		for (int i = 1; i < length; i++) {
    			tmp += "*";
    		}
    		
    		return tmp;
    	}
    	else{
    		return "";
    	}
	}
    
    /**
     * 真实姓名处理，如黄哈哈->黄*哈
     * @param contacts
     * @return
     */
    public static String contactsReplace2(String contacts) {
    	int length = contacts.length();
    	
    	if(length == 2){
    		return contacts.substring(0, 1) + "*";
    	}
    	
    	if(0 != length){
    		String tmp = contacts.substring(0, 1);
    		
    		for (int i = 1; i < length - 1; i++) {
    			tmp += "*";
    		}
    		
    		if(length > 1){
    			tmp += contacts.substring(length - 1, length);
    		}
    		
    		return tmp;
    	}
    	else{
    		return "";
    	}
	}
    
    /**
     * 手机号处理，如12345678901->123****8904
     * @param contacts
     * @return
     */
    public static String phoneReplace(String phone) {
    	if(phone == null || phone.length() != 11)
    	{
    		return phone;
    	}
    	else
    	{
    		StringBuilder sb = new StringBuilder(phone);
    		sb.replace(3,  7, "****");
    		return sb.toString();
    	}
	}
    
    /**
     * 卡号后四位
     * @param contacts
     * @return
     */
    public static String cardNoHandle(String cardNo) {
    	return cardNo.substring(cardNo.length() - 4);
	}
    
    /**
     * 整形金额格式化成12,000;
     * @param amount
     * @return
     */
    public static String amountFormat(Integer amount) {
    	NumberFormat nf = new DecimalFormat(",###");
    	
    	if(null == amount)
    	{
    		return "";
    	}
    	
    	return nf.format(amount);
	}
    
    /**
     * 卡号中间隐藏+“ ”，1234 **** **** 5678
     * @return
     */
    public static String cardNoHandleCenter(String cardNo){
    	int length = cardNo.length();
    	return cardNo.substring(0, 4) + " **** **** " + cardNo.substring(length - 4, length);
    }
    
    /**
     * 字符串转码
     * @return
     */
    public static String toUtf8(String str){
    	try {
			return new String("str".getBytes(),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return "";
    }
    /**
     * 四要素认证空格空行处理
     * @return
     */

	public static String replaceKgkh(String str) {
		String destination = "";
		if (str != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			destination = m.replaceAll("");
		}
		return destination;
	}
	
	public static String replaceBlank(String str) {
		String dest = "";
		if (str!=null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}

	/**
	 * 将版本字符串转换为整形
	 * @param version
	 * @return
	 */
	public static Integer versionStrToInt(String version){
		Integer versionInt = 0;
		if(null != version && !"".equals(version)){
			version = version.replace(".","");
			versionInt = Integer.parseInt(version);
		}
		return versionInt;
	}
	
	/**
	 * merchantCode新增cardezu前缀判断，如果已经有了就不加
	 * @param str
	 * @return
	 */
	public static String ifAddcardezuToStr(String str) {
		if (str.startsWith("cardezu")) {
			return str;
		}else {
			return "cardezu" + str;
		}
	}

	/**
	 * 去除小数 后0.只针对末尾是两位有效数字的
	 * @param strZero
	 * @return
			 */
	public static String decimalZero(String strZero) {
		String[] strZeroArray =  strZero.split("\\.");
		if(strZeroArray[1].equals("00")){
			strZero = strZeroArray[0];
		}else if(strZeroArray[1].endsWith("0")){
			strZero = strZeroArray[0]+"."+strZeroArray[1].substring(0,1);
		}
		return strZero;
	}

	/**
	 * 使用java正则表达式去掉多余的.与0
	 * @param s
	 * @return
	 */
	public static String subZeroAndDot(String s){
		if(s.indexOf(".") > 0){
			s = s.replaceAll("0+?$", "");//去掉多余的0
			s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
		}
		return s;
	}
}
